/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function JTableBooking() {
    this.tableId = "";
    this.count = 0;
    this.columns = new Array();
    this.dataPageSize = 20;
    this.pageLink = "";
    this.delfunc = "";
    this.edifunc = "";
    this.dataId = "";
    this.fields = new Array();
    this.offset =0;
    this.vCols = "";
    this.deleteEnable = false;
    this.editEnable = false;
}
JTableBooking.prototype.setEditEnable = function(enab) {
    this.editEnable = enab;
}
JTableBooking.prototype.setDeleteEnable = function(enab) {
    this.deleteEnable = enab;
}
JTableBooking.prototype.getEditEnable = function() {
    return this.editEnable;
}
JTableBooking.prototype.getDeleteEnable = function() {
    return this.deleteEnable
}
JTableBooking.prototype.setAlignment = function(rCols) {
    this.vCols = rCols;
}
JTableBooking.prototype.setTableId = function(vtableId) {
    this.tableId = vtableId;
}
JTableBooking.prototype.setCount = function(vcount) {
    this.count = vcount;
}
JTableBooking.prototype.setColumns = function(vcolumns) {
    this.columns = vcolumns;
}
JTableBooking.prototype.setDataPageSize = function(vDataPageSize) {
    this.dataPageSize = vDataPageSize;
}
JTableBooking.prototype.setPageLink = function(vPageLink) {
    this.pageLink = vPageLink;
}
JTableBooking.prototype.setDeleteFunction = function(vDelfunc) {
    this.delfunc = vDelfunc;
}
JTableBooking.prototype.setEditFunction = function(vEdifunc) {
    this.edifunc = vEdifunc;
}
JTableBooking.prototype.setDataId = function(vDataId) {
    this.dataId = vDataId;
}
JTableBooking.prototype.setFields = function(vFields) {
    this.fields = vFields;
}
JTableBooking.prototype.setOffset = function(vOffset) {
    this.offset = vOffset;
}
JTableBooking.prototype.setTableHeader = function() {
    try {
        this.removeTableHead();
        this.removeTableFooter();
        this.insertTableHead();
        var i =0;
        var footer = new Array();
        for(i =0; i< this.columns.length;i++) {
            footer[i] = "&nbsp;";
        }
        this.insertTableFoot(footer);
    } catch(err) {
        alert(err);
    }
}
JTableBooking.prototype.defaultValue = function(vals) {
    return ( vals == null ) ? vals+ "" : vals;
}
JTableBooking.prototype.loadData = function(list) {
    try {
        this.removeRowAll();
        var buf = new StringBuffer();
        buf.append("data[");
        buf.append(0);
        buf.append("]=this.defaultValue(obj.");
        buf.append(this.dataId);
        buf.append(");");
        for(var i = 0, j = 1; i < this.fields.length; i++, j++) {
            buf.append("data[");
            buf.append(j);
            buf.append("]=this.defaultValue(obj.");
            buf.append(this.fields[i]);
            buf.append(");");
        }
        var str_schema = buf.toString();
        this.convert(list, str_schema);
    } catch(err) {
        alert(err);
    }
}
JTableBooking.prototype.convert = function(list,schema) {
    try {
        for(var i = 0; i < list.size(); i++) {
            var obj = list.get(i);
            var data = new Array();
            eval(schema);
            var index = (this.offset+1) + i;
            this.insertTableRow(data, index);
        }
    } catch(err) {
        alert(err);
    }
}
JTableBooking.prototype.getTable = function() {
    return document.getElementById(this.tableId);
}
JTableBooking.prototype.getTableBody = function() {
    var theTable = this.getTable();
    return theTable.tBodies[0];
}
JTableBooking.prototype.insertTableHead = function() {
    var dcount = 0;
    var ccid = 0;
    if( this.dataPageSize >= this.count ) {
        dcount = 1;
    } else {
        dcount = parseInt(this.count / this.dataPageSize);
        ccid = this.count % this.dataPageSize;
    }
    
    var num_pages = 0;
    if( dcount > 0 ) {
        num_pages = dcount;
    }
    if( ccid > 0 ) {
        num_pages += 1;
    }
    var buf = new StringBuffer();
    buf.append("Total:"+ this.count);
    buf.append("&nbsp;&nbsp;");
    var i = 0;
    var j = 0;
    for(i=0; i< num_pages; i++) {
        var p = i + 1;
        buf.append("&nbsp;<a href=\"javaScript:void(0)\" onclick=\"");
        buf.append(this.pageLink);
        var lk = i * this.dataPageSize;
        buf.append("("+ lk +")");
        buf.append("\">");
        buf.append(""+p);
        buf.append("</a>");
    }
    var theTable = this.getTable();
    var newCell;
    var newTHEAD = theTable.createTHead();
    newTHEAD.id = this.tableId+"Header";
    var barrow = newTHEAD.insertRow(-1);
    newCell = barrow.insertCell(0);
    newCell.innerHTML = buf.toString();
    newTHEAD.rows[0].cells[0].setAttribute("colspan", this.columns.length+3);
    var newRow = newTHEAD.insertRow(-1);
    /*
    for (i = 0; i < 3; i++) {
        newCell = newRow.insertCell(i);
        newCell.innerHTML = "&nbsp;";
        newTHEAD.rows[1].cells[i].setAttribute("class", "tableHead");
    }*/
    for (i = 0, j = 0; i < this.columns.length; i++, j++) {
        newCell = newRow.insertCell(j);
        newCell.innerHTML = this.columns[i];
        newTHEAD.rows[1].cells[j].setAttribute("class", "tableHead");
    //newTHEAD.rows[0].cells[i].className ="lookupTableHeader";
    //newTHEAD.rows[0].cells[i].style.backgroundColor="#999999";
    //newTHEAD.rows[0].cells[i].style.textAlign="center";
    //newTHEAD.rows[0].cells[i].style.fontStyle="oblique";
    }
}
JTableBooking.prototype.insertTableRow = function(nowData,index) {
    var theTableBody = this.getTableBody();
    var newCell;
    var where = theTableBody.rows.length;
    var newRow = theTableBody.insertRow(where);
    newRow.style.height = "2px";
    //var newCell0 = newRow.insertCell(0);
    //newCell0.style.textAlign = "center";
    //newCell0.innerHTML = ""+index;
    //var newCell1 = newRow.insertCell(1);
    //newCell1.style.textAlign = "center";
    //newCell1.innerHTML = "<img src='../imgs/delete16.png' align='center' onmouseover=\"icon_point(this)\" onclick=\""+this.delfunc+"('"+nowData[0]+"')\">";
    //var newCell2 = newRow.insertCell(2);
    //newCell2.style.textAlign = "center";
    //newCell2.innerHTML = "<img src='../imgs/edit16.png' align='center' onmouseover=\"icon_point(this)\" onclick=\""+this.edifunc+"('"+nowData[0]+"')\">";
   // if( (where % 2) == 1 ) {
   //     newCell0.style.backgroundColor ="#cccccc";
        //newCell1.style.backgroundColor ="#cccccc";
        //newCell2.style.backgroundColor ="#cccccc";
    //}
    
    var cols = new Array(); 
    var i = 0;
    for(i = 0; i < nowData.length; i++) {
        cols[i] = 'left';
    }
    if( this.vCols.length > 0 ) { 
        //alert(this.vCols);
        eval(this.vCols);
    }
    var j = 0;
    for (i = 1, j = 0; i < nowData.length; i++, j++) {
        newCell = newRow.insertCell(j);
        if( i == 1 || i == 2 ) {
            newCell.innerHTML = "<a href=\"javaScript:void(0)\" onclick=\"editBooking('"+nowData[0]+"')\">"+nowData[i]+"</a>";
        } else {
            newCell.innerHTML = nowData[i];
        }
        newCell.style.paddingRight = 5+"px";
        newCell.style.paddingLeft = 5+"px";
        //newCell.style.height = "2px";
         newCell.style.textAlign = cols[j];
        if( (where % 2) == 1 ) {
            newCell.style.backgroundColor ="#cccccc";
        }
    }
}

JTableBooking.prototype.removeRow = function(index) {
    var theTableBody = this.getTableBody();
    theTableBody.deleteRow(index);
}
JTableBooking.prototype.removeRowAll = function() {
    var theTableBody = this.getTableBody();
    var rcount = theTableBody.rows.length;
    for (var i = 0; i < rcount; i++) {
        theTableBody.deleteRow(0);
    }
}
JTableBooking.prototype.removeTableHead = function() {
    document.getElementById(this.tableId).deleteTHead();
}
JTableBooking.prototype.removeTableFooter = function() {
    document.getElementById(this.tableId).deleteTFoot();
}
JTableBooking.prototype.insertTableFoot = function(TFOOTData) {
    var theTable = this.getTable();
    var newCell
    var newTFOOT = theTable.createTFoot()
    newTFOOT.id = this.tableId+"Footer";
    var newRow = newTFOOT.insertRow(-1)
    for (var i = 0; i < TFOOTData.length; i++) {
        newCell = newRow.insertCell(i);
        newCell.innerHTML = TFOOTData[i];
        newCell.setAttribute("class", "tableFoot");
    }
}

